/* Name: RadioBtn
 * Author: John Gideon
 * Date: 5-29-12
 * Description: Used to create radio button, so user can select from several options
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.LinkedList;

public class RadioBtn extends Button {
	private int width;
	private int height;
	private String text = "";
	private LinkedList<RadioBtn> links = new LinkedList<RadioBtn>();
	
	// Creates a radio button at the given location, with the given dimensions
	public RadioBtn(int px, int py, int pwidth, int pheight) {
		super(px, py, pwidth, pheight);
		Game.create((Item)this, 1); // Needed for update, draw, and destroy
		colType = COL_NONE; // Needed for collision checking
		width = pwidth;
		height = pheight;
	}
	
	// Called when the button is pressed - selects selected button, and deselects others 
	protected void pressed() {
		Sound.effect(Sound.EFFECT_CLICK);
		for(Iterator it = links.iterator(); it.hasNext();) {
			RadioBtn id = (RadioBtn) it.next();
			id.selected = false;
		}
	}
	
	// Called when button is released
	protected void released() {
		selected = true;
	}
	
	// Called when new frames are drawn on screen - properly displays buttons
	public void draw() {
		Graphics2D gh = Game.getGraphicsHandle();
		if (btnDrawState == Button.BTN_STATE_OFF) {gh.setColor(Color.GRAY);}
		else if (btnDrawState == Button.BTN_STATE_HOVER) {gh.setColor(Color.LIGHT_GRAY);}
		else {gh.setColor(Color.DARK_GRAY);}
		gh.fillRoundRect(x, y, width, height, 15, 15);
		gh.setFont(new Font("Serif", Font.PLAIN, 40));
		gh.setColor(Color.GREEN);
		FontRenderContext frc = gh.getFontRenderContext();
		Rectangle2D rect = gh.getFont().getStringBounds(text, frc);
		gh.drawChars(text.toCharArray(), 0, text.length(), (int)(x + width/2 - rect.getWidth()/2), (int)(y + height/2 + rect.getHeight()/2)-10);
	}
	
	// Sets the button text
	public void setText(String pText) {text = pText;}
	
	// Used to link multiple radio buttons together
	public void addLink(RadioBtn val) {
		links.add(val);
	}
	
	public void update() {updateBtn();}
	public void leftMousePress() {leftMousePressBtn();}
	public void leftMouseRelease() {leftMouseReleaseBtn();}
}
